// 网关配置的前缀
var companyServer = "/companySer"


var currentPage = 1;    // 当前页
var pageSize = 10;      // 页大小
var total = 0;          // 数据总量
var pages = 1;          // 页数总量
var search_value = "";

let Authorization = "";
let refreshToken = "";

// 加载页面时读取后台的 ip
window.onload = function () {
    let accountStatus = $("#accountStatus").html();
    if(accountStatus !== "正常"){
        $("#forbidden").removeAttr("hidden");
    }

    let authorities = $("#authorities").html();
    unauthorized_2(authorities, "companyManage");

    $.ajaxSettings.async = false;
    $.getJSON("/js/properties.json", function (data) {
        companyServer = data.companyServer;
    });
    // 获取令牌
    Authorization = getCookie("Authorization")
    refreshToken = getCookie("refreshToken")
    getCompanies(currentPage, pageSize, search_value);
}

function getCompanies(currentPage, pageSize, search_value) {
    $.ajax({
        type: "get",
        async: false,
        url: companyServer + "/company/companyInfo",
        data: {
            currentPage: currentPage,
            pageSize: pageSize,
            searchValue: search_value,
        },
        success: function (data) {
            unauthorized(data);
            console.log("所有的企业数据 - 第 " + currentPage + " 页");
            total = data.datas.total;
            pageSize = data.datas.size;
            pages = data.datas.pages;

            //清空数据
            $("#companyInfo").empty();
            //返回的数据用data.rows获取内容
            $.each(data.datas.records, function (i, item) {
                let companyStatus = "";
                let option = "";
                let button_1 = "<button onclick='updateCompanyStatus(\"" + item.companyId + "\", \"正常\")' type='button' class='btn btn-success btn-sm'>恢复运营</button>";
                let button_2 = "<button onclick='updateCompanyStatus(\"" + item.companyId + "\", \"暂停\")' type='button' class='btn btn-warning btn-sm'>暂停运营</button>";
                let button_3 = "<button onclick='updateCompanyStatus(\"" + item.companyId + "\", \"停止\")' type='button' class='btn btn-danger btn-sm'>停止运营</button>";

                if (item.companyStatus === "正常") {
                    companyStatus = "<span class='approved'>正常</span>";
                    option = button_2 + "&nbsp;&nbsp;" + button_3;
                } else if (item.companyStatus === "暂停") {
                    companyStatus = "<span class='pending'>暂停</span>";
                    option = button_1 + "&nbsp;&nbsp;" + button_3;
                } else if (item.companyStatus === "停止") {
                    companyStatus = "<span class='rejected'>停止</span>";
                    option = "<button type='button' onclick='deleteCompany(\"" + item.companyId + "\")' class='btn btn-primary btn-sm'>删除</button>";
                }


                $("#companyInfo").append(
                    "<tr class='job-application-item'>" +
                    "<td class='application-job'>" +
                    "<h3><a onclick='companyDetail(\""+item.companyId+"\")'>" + item.companyName + "</a></h3>" +
                    "</td>" +

                    "<td class='application-employer'>" +
                    "<a class='dotted'>" + item.memberLevel + "</a>" +
                    "</td>" +

                    "<td class='application-created'>" +
                    "<span> " + item.inDate + " </span>" +
                    "</td>" +

                    "<td class='status'>" +
                    companyStatus +
                    "</td>" +

                    "<td class='view-application-pop text-right'>" +
                    option +
                    "</td>" +
                    "</tr>"
                )

            });

            // pageMe.js 使用方法
            $("#page").paging({
                pageNum: currentPage, // 当前页面
                totalNum: pages, // 总页码
                totalList: total, // 记录总数量
                pageSize: pageSize,
                callback: function (num, size) { //回调函数
                    pageSize = size;
                    currentPage = num;
                    getCompanies(num, size, search_value);
                    // 返回顶部
                    if ($(window).scrollTop() > 200) {
                        $('body,html').animate({
                            scrollTop: 0
                        });
                    }

                }
            });
        },
        error: function (err) {
            cocoMessage.error(2000, err.statusText, function () {
                console.log(err);
            });
        }

    });

}

//  搜索框
function click_search() {
    var tmp = $('#company_search').val();
    if (tmp === null || tmp === "") {
        search_value = "";
    } else {
        search_value = tmp;
    }
    getCompanies(1, pageSize, search_value);
}

function updateCompanyStatus(companyId, status) {
    $.confirm({
        title: '提示',
        content: '确认修改该企业状态吗？',
        type: 'green',
        theme: "modern",
        closeAnimation: 'scaleY',
        buttons: {
            ok: {
                text: '确认',
                btnClass: 'btn-warning',
                action: function () {
                    $.ajax({
                        type: "POST",
                        async: false,
                        url: companyServer + "/company/companyInfo",
                        headers: {
                            "Content-Type": "application/json",
                            "Authorization": Authorization,
                            "refreshToken": refreshToken,
                        },
                        data: JSON.stringify({
                            "companyId": companyId,
                            "companyStatus": status
                        }),
                        success: function (data) {
                            unauthorized(data);
                            if (data.resp_code === 0) {
                                // 成功
                                cocoMessage.info(2000, data.resp_msg, function () {
                                });
                                location.reload();
                            } else {
                                // 失败
                                cocoMessage.error(2000, data.resp_msg, function () {
                                    console.log(data.resp_msg);
                                });
                            }
                        },
                        error: function (err) {
                            // 异常
                            cocoMessage.error(2000, err.statusText, function () {
                                console.log(err);
                            });
                        }
                    })
                }
            },
            cancel: {
                text: '取消',
                btnClass: 'btn-default'
            }
        }
    });
}

function deleteCompany(companyId) {
    $.confirm({
        title: '提示',
        content: '确认删除改企业吗？',
        type: 'green',
        theme: "modern",
        closeAnimation: 'scaleY',
        buttons: {
            ok: {
                text: '确认',
                btnClass: 'btn-warning',
                action: function () {
                    $.ajax({
                        type: "DELETE",
                        async: false,
                        url: companyServer + "/company/companyInfo?companyId=" + companyId,
                        headers: {
                            "Content-Type": "application/json",
                            "Authorization": Authorization,
                            "refreshToken": refreshToken,
                        },
                        success: function (data) {
                            console.log(data);
                            if (data.resp_code === 0) {
                                // 成功
                                cocoMessage.info(2000, data.resp_msg, function () {
                                    console.log(data.resp_msg);
                                });
                                location.reload();
                            } else {
                                // 失败
                                cocoMessage.error(2000, data.resp_msg, function () {
                                    console.log(data.resp_msg);
                                });
                            }
                        },
                        error: function (err) {
                            // 异常
                            cocoMessage.error(2000, err.statusText, function () {
                                console.log(err);
                            });
                        }
                    })
                }
            },
            cancel: {
                text: '取消',
                btnClass: 'btn-default'
            }
        }
    });
}

function companyDetail(companyId){
    location.href ="/oa/company/companyDetailsPage/"+companyId;
}
